﻿Imports System.Activities
Imports System.Windows.Data
Imports System.Activities.Expressions

''' <summary>InArgument と Boolean 値を変換する</summary>
''' <remarks>http://blogs.msdn.com/b/tilovell/archive/2011/04/06/wf4-showing-an-inargument-lt-bool-gt-as-a-checkbox-in-the-workflow-designer-property-grid.aspx</remarks>
Public Class InArgumentToBoolConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim resultValue As Boolean = Nothing
        If TypeOf value Is InArgument(Of Boolean) Then
            Dim exp = DirectCast(value, InArgument(Of Boolean)).Expression
            If TypeOf exp Is Literal(Of Boolean) Then
                resultValue = DirectCast(exp, Literal(Of Boolean)).Value
            End If
        End If
        Return resultValue
    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Dim resultValue As InArgument(Of Boolean) = Nothing
        If TypeOf value Is Boolean Then
            resultValue = New InArgument(Of Boolean)(
                                     New Literal(Of Boolean)(
                                         CType(value, Boolean)))
        End If
        Return resultValue
    End Function
End Class
